package goulu.bloggy.com.Activities;

import goulu.bloggy.com.R;
import goulu.bloggy.com.Connections.PostMysqlConnection;
import goulu.bloggy.com.Helpers.PostHelper;
import goulu.bloggy.com.Models.Post;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class EditPostActivity extends Activity implements OnClickListener
{
	private EditText title;
	private EditText content;
	private TextView moodText;
	private ImageView moodImage;
	private String mood;
	private CheckBox published;
	private Button submitBT;
	private Button cancelBT;
	private Button moodBT;
	private Post currentPost;
	Dialog dialog;
	
    public OnClickListener moodListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.moodImageButtonPlain:
				mood = "plain";
				break;
			case R.id.moodImageButtonHappy:
				mood = "happy";
				break;
			case R.id.moodImageButtonEmbarrassed:
				mood = "embarrassed";
				break;
			case R.id.moodImageButtonJoy:
				mood = "joy";
				break;
			case R.id.moodImageButtonLaugh:
				mood = "laugh";
				break;
			case R.id.moodImageButtonSad:
				mood = "sad";
				break;
			default:
				mood = null;
				break;
			}
			if(mood != null) {
				moodText.setText(mood);
				moodImage.setImageResource(PostHelper.getMoodResourceId(mood));
				dialog.dismiss();
			}
		}
	};
	
	public OnClickListener moodListListener = new OnClickListener() {
		
		public void onClick(View v) {
			if(v.getId() == R.id.moodListButtonCancel) {
				mood = null;
				moodText.setText("None");
				moodImage.setImageResource(0);
				dialog.cancel();
			}
		}
	};
	
    Handler postHandler = new Handler()
    {

		@Override
		public void handleMessage(Message msg) 
		{
			if(msg.what == PostMysqlConnection.UPDATE_POST)
			{
				Log.d("EditPostActivity.handleMessage()", "updated post, giving updated post back");
				Toast.makeText(getApplicationContext(), msg.obj.toString(), Toast.LENGTH_LONG).show();
				Intent data = new Intent();
				data.putExtra("post", currentPost);
				setResult(PostHelper.CONTENT_CHANGED, data);
				finish();
			}
		}
    	
    };
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newpostview);
        setupView();
    }

	private void setupView() 
	{
		title = (EditText)findViewById(R.id.newpostPostTitle);
		content = (EditText)findViewById(R.id.newpostPostContent);
		published = (CheckBox)findViewById(R.id.newpostPublishedBox);
		moodImage = (ImageView)findViewById(R.id.newpostMoodImage);
		moodText = (TextView)findViewById(R.id.newpostMoodText);
		
		Intent intent = getIntent();
		currentPost = (Post)intent.getSerializableExtra("post");
		title.setText(currentPost.getTitle());
		content.setText(currentPost.getContent());
		published.setChecked(currentPost.isPublished());
		mood = currentPost.getMood();
		moodImage.setImageResource(PostHelper.getMoodResourceId(currentPost.getMood()));
		
		submitBT = (Button)findViewById(R.id.newpostSaveButton);
		submitBT.setOnClickListener(this);
		cancelBT = (Button)findViewById(R.id.newpostCancelButton);
		cancelBT.setOnClickListener(this);
		moodBT = (Button)findViewById(R.id.newpostMoodButton);
		moodBT.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) 
	{
		switch (v.getId()) {
		case R.id.newpostSaveButton:
			putInputToPost();
			PostMysqlConnection conn = new PostMysqlConnection(postHandler, PostMysqlConnection.UPDATE_POST, currentPost);
			Thread t = new Thread(conn);
			t.start();
			break;
		case R.id.newpostCancelButton:
			setResult(PostHelper.NOTHING_CHANGED);
			finish();
			break;
		case R.id.newpostMoodButton:
			showMoodDialog();
			break;

		}
		
	}

	private void putInputToPost() 
	{
		currentPost.setTitle(title.getText().toString());
		currentPost.setContent(content.getText().toString());
		currentPost.setMood(mood);
		currentPost.setPublished(published.isChecked());	
	}
	
	public void showMoodDialog() {
		
		dialog = new Dialog(this);
		
		dialog.setContentView(R.layout.listmoodview);
		dialog.setTitle("Select mood");
		dialog.setCancelable(true);

		ImageButton imgButtonPlain = (ImageButton)dialog.findViewById(R.id.moodImageButtonPlain);
		imgButtonPlain.setOnClickListener(moodListener);
		ImageButton imgButtonHappy = (ImageButton)dialog.findViewById(R.id.moodImageButtonHappy);
		imgButtonHappy.setOnClickListener(moodListener);
		ImageButton imgButtonEmbarrassed = (ImageButton)dialog.findViewById(R.id.moodImageButtonEmbarrassed);
		imgButtonEmbarrassed.setOnClickListener(moodListener);
		ImageButton imgButtonJoy = (ImageButton)dialog.findViewById(R.id.moodImageButtonJoy);
		imgButtonJoy.setOnClickListener(moodListener);
		ImageButton imgButtonLaugh = (ImageButton)dialog.findViewById(R.id.moodImageButtonLaugh);
		imgButtonLaugh.setOnClickListener(moodListener);
		ImageButton imgButtonSad = (ImageButton)dialog.findViewById(R.id.moodImageButtonSad);
		imgButtonSad.setOnClickListener(moodListener);
		Button moodButtonCancel = (Button)dialog.findViewById(R.id.moodListButtonCancel);
		moodButtonCancel.setOnClickListener(moodListListener);
		
		dialog.show();
	}
}